//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    articleList: [],
    page: 1,
    isLoadMore: true,
    isLoadFinish: false,
  },

  onLoad: function () {
    this.getArticleList();
  },
  //文章列表
  getArticleList: function () {
    var self = this;
    var page = self.data.page
    wx.request({
      url: 'http://118.24.183.152/XiaoChengXu/article/list',
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        page: page
      },
      success: function (res) {
        var code = res.data.code;
        if (code == 0) {
          //有数据返回
          var datas = res.data.data;
          var listTemp = self.data.articleList;
          listTemp = listTemp.concat(datas);
          self.setData({
            articleList: listTemp,
            isLoadMore: true,
          });
        } else {
          self.setData({
            isLoadMore: false,
            isLoadFinish: true,
          });
        }

      },
      fail: function () {

      },

    })
  },
  //跳转详情页
  onItemClick: function (e) {
    var index = e.currentTarget.dataset.index;
    var articleId = this.data.articleList[index].id;
    var title = this.data.articleList[index].title;

    //跳转页面
    wx.navigateTo({
      url: '/pages/detail/detail?articleId=' + articleId + '&title=' + title,
    })

  },
  //滚动到底部触发事件  
  onLoadMore: function () {
    var self = this;

    if (self.data.isLoadMore && !self.data.isLoadFinish) {
      self.setData({
        page: self.data.page + 1,  //每次触发上拉事件，把 page + 1  
      });
      self.getArticleList();
    }

  }
})
